import app.tools.sm_config_reader as sm_type
import app.models.dao as dao

LayerType = sm_type.LayerType
mine_id = sm_type.default_MineId

# 当 devicemodel中还没有从kj83上同步位置数据的时候，此处进行初始化
async def position_init():
    positions = await dao.query_all_position()
    format_positions = [list(item) for item in positions]
    admin_sensors = await get_admin_sensors_data()
    for p in format_positions:
        p.append(None)
        for admin_data in admin_sensors:
            if p[1] == admin_data['node_id']:
                p[-1] = admin_data['node_type']
                break
    dao.smdeviceinit(format_positions)


# 联表查询 实现原本 view_broadcast_data 视图的效果。
async def get_broadcast_base_data():
    data = await dao.query_broadcast_base_data()
    broadcast_data = list()
    for b in data:
        broadcast = dict()
        broadcast['node_id'] = b[0]
        broadcast['state'] = b[1]
        broadcast['last_update_time'] = b[2].ctime()
        broadcast['alarm'] = b[3]
        broadcast['place'] = b[4]
        broadcast['node_type'] = b[5]
        broadcast_data.append(broadcast)
    return broadcast_data

# 自己进行联表查询，实现原本 view_gas_data 视图的效果
async def get_sensor_base_data():
    data = await dao.query_sensor_base_data()
    sensor_data = list()
    for s in data:
        sensor = dict()
        sensor['node_id'] = s[0]
        sensor['state'] = s[1]
        sensor['alarm'] = s[2]
        sensor['real_data'] = s[3]
        sensor['last_alarm_date'] = s[4].ctime()
        sensor_data.append(sensor)
    return sensor_data


# 获取摄像头的
async def get_video_base_data():
    data = await dao.query_video_base_data()
    print('原始的video-data', data)
    video_data = list()
    for v in data:
        video = dict()
        video['node_id'] = v[0]
        video['place'] = v[1]
        video['manufacture'] = v[2]
        video['ip'] = v[3]
        video['port'] = v[4]
        video['username'] = v[5]
        video['password'] = v[6]
        video['channel'] = v[7]
        video['node_type'] = v[8]
        video_data.append(video)
    return video_data


def get_sm_device_positions():
    positions = dao.query_sm_device_positions()
    return positions


async def get_admin_broadcast_data():
    broads = []
    broads_rows = await dao.query_broadcast_admin(mine_id)
    for row in broads_rows:
        broad = {
            'node_id': row[1],
            'node_type': row[3],
            'place': row[4]
        }
        broad['name'] = " - ".join([broad['node_id'], broad['place'], broad['node_type']])
        broads.append(broad)
    return broads


async def get_admin_videos_data():
    videos = []
    video_rows = await dao.query_video_admin()
    for row in video_rows:
        video = {
            'node_id': row[1],
            'node_type': row[3],
            'place': row[4],
            'manufacture': row[6],
            'ip': row[7], 'port': row[8],
            'username': row[9],
            'password': row[10],
            'channel': row[12],
            'url': row[13]
        }
        video['name'] = " - ".join([video['place'], video['node_type']])
        videos.append(video)
    return videos


async def get_admin_sensors_data():
    gass = []
    gas_rows = await dao.query_gas_admin()
    for row in gas_rows:
        gas = {
            'node_id': row[1],
            'parent_id': row[2],
            'node_type': row[3],
            'place': row[8]
        }
        # gas['is_parent'] = False
        gas['name'] = " - ".join([str(gas['node_id']), str(gas['place']), str(gas['node_type'])])
        gass.append(gas)
    return gass


async def get_broads_infos():
    infos = await dao.query_broadcast_infos()
    broadcast_infos = list()
    for info in infos:
        broadcast_info = dict()
        broadcast_info['node_id'] = info[0]
        broadcast_info['node_type'] = info[1]
        broadcast_info['place'] = info[2]
        broadcast_info['last_update_time'] = info[3].ctime()
        broadcast_info['state'] = info[4]
        broadcast_info['alarm'] = info[5]
        broadcast_infos.append(broadcast_info)
    return broadcast_infos


async def get_video_infos():
    infos = await dao.query_video_infos()
    video_infos = list()
    for info in infos:
        video_info = dict()
        video_info['node_id'] = info[0]
        video_info['node_type'] = info[2]
        video_info['place'] = info[3]
        video_info['manufacture'] = info[5]
        video_info['ip'] = info[6]
        video_info['port'] = info[7]
        video_info['username'] = info[8]
        video_info['password'] = info[9]
        video_info['channel'] = info[11]
        video_info['url'] = info[12]
        video_infos.append(video_info)
    return video_infos


async def get_gas_infos():
    infos = await dao.query_gas_infos()
    sensor_infos = list()
    for info in infos:
        sensor_info = dict()
        sensor_info['node_id'] = info[0]
        sensor_info['node_type'] = info[2]
        sensor_info['alarm_upper_bound'] = info[3]
        sensor_info['alarm_lower_bound'] = info[4]
        sensor_info['unit'] = info[5]
        sensor_info['place'] = info[6]
        sensor_info['real_data'] = info[7]
        sensor_infos.append(sensor_info)
    return sensor_infos


# 把多个设备的位置信息添加到 device表格中
def add_device_position(positions):
    new_positions = dao.add_device_positions(positions)
    return new_positions


# 同时更新 device表格中 多个位置的信息
def update_device_position(positions):
    new_positions = dao.update_device_positions(positions)
    return new_positions


def delete_device_position(positions):
    new_positions = dao.delete_device_positions(positions)
    return new_positions